﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace Dao
{
    public class ZL_CK_Dao
    {
        /// <summary>
        /// 新增一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Add(ZL_CK entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                context.ZL_CKSet.AddObject(entity);
                return context.SaveChanges();
            }
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Update(ZL_CK entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                ZL_CK entity2 = context.ZL_CKSet.FirstOrDefault(c => c.ID == entity.ID);
                if (entity2 != null)
                {
                    //entity2.BH = entity.BH;
                    //entity2.MC = entity.MC;
                    //entity2.WZ = entity.WZ;
                    //entity2.CKLXBH = entity.CKLXBH;
                    context.ZL_CKSet.ApplyCurrentValues(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>操作影响的行数</returns>
        public int Delete(string id)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                ZL_CK entity = context.ZL_CKSet.FirstOrDefault(c => c.ID == guid);
                if (entity != null)
                {
                    context.DeleteObject(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 查询一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>查询结果实体</returns>
        public ZL_CK Query(string id)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                ZL_CK entity = context.ZL_CKSet.FirstOrDefault(c => c.ID == guid);
                return entity;
            }
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <returns>查询结果实体</returns>
        public bool Exist(string bh, string mc)
        {
            using (var context = new Dao.ErpEntities())
            {
                ZL_CK entity = context.ZL_CKSet.FirstOrDefault(c => c.BH == bh || c.MC == mc);
                return entity != null;
            }
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <returns>查询结果实体</returns>
        public bool Exist(string mc)
        {
            using (var context = new Dao.ErpEntities())
            {
                ZL_CK entity = context.ZL_CKSet.FirstOrDefault(c => c.MC == mc);
                return entity != null;
            }
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<ZL_CK> QueryAll()
        {
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.ZL_CKSet
                        select c;

                List<ZL_CK> list = new List<ZL_CK>();
                foreach (var c in q)
                {
                    list.Add(c);
                }
                return list;
            }
        }

        /// <summary>
        /// 分布查询
        /// </summary>
        /// <param name="page">待查询页码</param>
        /// <param name="pageSize">每页条数</param>
        /// <returns>查询结果</returns>
        /// <remarks>简单查询时使用</remarks>
        public List<ZL_CK> QueryByPage(int page, int pageSize)
        {
            using (var context = new Dao.ErpEntities())
            {
                int start = page * pageSize;
                int end = page * pageSize + pageSize;
                ObjectQuery<ZL_CK> zlck = context.ZL_CKSet;
                IQueryable<ZL_CK> q = (from c in zlck
                                       orderby c.ID
                                       select c).Skip(start).Take(end);
                List<ZL_CK> list = new List<ZL_CK>();
                foreach (var c in q)
                {
                    list.Add(c);
                }

                return list;
            }
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page">待查询页码</param>
        /// <param name="pageSize">每页条数</param>
        /// <returns>查询结果</returns>
        /// <remarks>可用于复杂查询</remarks>
        public List<ZL_CK> QueryByPage2(int page, int pageSize)
        {
            using (var context = new Dao.ErpEntities())
            {
                string sql = "select value c from ErpEntities.ZL_CK as c order by c.ID skip {0} limit {1}";
                int start = page * pageSize;
                int end = page * pageSize + pageSize;
                string execSql = string.Format(sql, start, end);
                ObjectQuery<ZL_CK> query = context.CreateQuery<ZL_CK>(execSql);
                List<ZL_CK> list = new List<ZL_CK>();
                foreach (ZL_CK entity in query)
                {
                    list.Add(entity);
                }
                return list;
            }
        }

        /// <summary>
        /// TODO 用存储过程提高效率
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<ZL_CK> QueryBySql(string bh, string mc, string lxbh)
        {
            List<ZL_CK> list = new List<ZL_CK>();
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.ZL_CKSet
                        where c.BH.Contains(bh)
                        select c;

                if (!string.IsNullOrEmpty(mc))
                {
                    q = q.Where(c => c.MC.Contains(mc));
                }

                if (!string.IsNullOrEmpty(lxbh) && (!"0".Equals(lxbh)))
                {
                    q = q.Where(c => c.CKLXBH == lxbh);
                }

                foreach (ZL_CK c in q)
                {
                    list.Add(c);
                }

                return list;
            }
        }
    }
}
